﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.BusinessObjects;
using System.Data.Common;

namespace SCRUM.Core.DataProviders
{
    /// <summary>
    /// Privider for comunication with the database
    /// </summary>
    public partial class DBDataProvider : DataProvider
    {
        /// <summary>
        /// Gets the widgets for the specified column.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <returns>
        /// List of widgets for the specified column.
        /// </returns>
        public override IList<Widget> GetWidgets(WidgetColumn column)
        {
            IList<Widget> widgets = new List<Widget>();
            try
            {
                DbParameter columnID = CreateParameter("columnID", column.ID);

                connection.Open();
                DbDataReader data = GetVector("widgets/GetWidgets", columnID);

                while (data.Read())
                {
                    Widget widget = new Widget(
                        (int)data["ID"],
                        (string)data["TYPE"],
                        (int)data["ORDER"],
                        (int)data["TYPE_ID"]);
                    widgets.Add(widget);
                }
            }
            finally
            {
                connection.Close();
            }
            return widgets; 
        }

        /// <summary>
        /// Gets the widget by ID.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>Widget with a specified ID.</returns>
        public override Widget GetWidgetByID(int id)
        {
            Widget widget = null;
            try
            {
                DbParameter widgetID = CreateParameter("widgetID", id);

                connection.Open();
                DbDataReader data = GetVector("widgets/GetWidgetByID", widgetID);

                if (data.Read())
                {
                    widget = new Widget(
                        (int)data["ID"],
                        (string)data["TYPE"],
                        (int)data["ORDER"],
                        (int)data["TYPE_ID"]);
                }
            }
            finally
            {
                connection.Close();
            }
            return widget; 
        }

        /// <summary>
        /// Creates the new instance of widget for specified user
        /// </summary>
        /// <param name="widget">The widget.</param>
        /// <param name="user">The user.</param>
        public override void CreateNewWidget(int widgetTypeID, int order, int widgetColumnID, int userID)
        {
            try
            {
                //DbParameter dbWidgetID = CreateParameter("widgetID", id);
                DbParameter dbColumnID = CreateParameter("columnID",widgetColumnID);
                DbParameter dbTypeID = CreateParameter("typeID",widgetTypeID);
                DbParameter dbOrder = CreateParameter("order",order);
                DbParameter dbUserID = CreateParameter("userID",userID);

                connection.Open();
                ExecuteNonQueryTransaction("widgets/CreateNewWidget", dbTypeID, dbUserID, dbColumnID, dbOrder);
                
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Deletes the widget.
        /// </summary>
        /// <param name="widget">The widget.</param>
        public override void DeleteWidget(Widget widget)
        {
            try
            {
                DbParameter dbWidgetID = CreateParameter("widgetID", widget.ID);
                DbParameter dbOrder = CreateParameter("order", widget.Order);
                connection.Open();
                ExecuteNonQueryTransaction("widgets/DeleteWidget", dbWidgetID, dbOrder);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Changes the order of the widget in column. 
        /// </summary>
        /// <param name="widget">Widget to change order</param>
        /// <param name="up">Direction, if true then increase order, else decrease</param>
        public override void ChangeWidgetOrder(Widget widget, bool up)
        {
            try
            {
                
                IList<Widget> widgets = GetWidgets(widget.Column);
                connection.Open();
                //direction is up and is not the last one
                if (up)
                {
                    if (widget.Order != 0)
                    {
                        DbParameter dbWidget1ID = CreateParameter("widget2ID", widget.ID);
                        Widget widget2 = widgets[widget.Order - 1];
                        DbParameter dbWidget2ID = CreateParameter("widget1ID", widget2.ID);
                        ExecuteNonQueryTransaction("widgets/SwitchWidgets", dbWidget1ID, dbWidget2ID);
                    }
                } 
                else
                {
                    if (widget.Order != widgets.Count - 1)
                    {
                        DbParameter dbWidget1ID = CreateParameter("widget1ID", widget.ID);
                        Widget widget2 = widgets[widget.Order + 1];
                        DbParameter dbWidget2ID = CreateParameter("widget2ID", widget2.ID);
                        ExecuteNonQueryTransaction("widgets/SwitchWidgets", dbWidget1ID, dbWidget2ID);
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Copies the widgets into column.
        /// </summary>
        /// <param name="widgetsToCopy">The widgets to copy.</param>
        /// <param name="columnID">The column ID.</param>
        /// <param name="userID">The user ID.</param>
        public override void CopyWidgetsIntoColumn(IList<Widget> widgetsToCopy, int columnID, int userID)
        {
            for (int i = 0; i < widgetsToCopy.Count; i++)
            {
                CreateNewWidget(widgetsToCopy[i].TypeID, widgetsToCopy[i].Order, columnID, userID);
            }
        }
    }
}
